clear all
% path1='C:\Users\Tom�s\Desktop\Temp\';
path1='C:\Users\Tom�s\Documents\1. Regehr Lab\Data summary\CC paper input output curves\';


%run readCCxlslx script first to get spikes variable into workspace or load
%from mat file
load([path1 'CC_Spikes.mat'])
VStepsCC=VSteps;
clear VSteps
%try first to run all data, not sure if it will work due to different
%Vstep increments for CCs (5 pA vs 10 for MLI)
spikeMat3=CCIOMat;




%run readMLIxlsx script first to get spikes variable on workspace, here I
%saved the spikes and Vsteps for MLI in a .mat file so I'm just going to
%load it, 
load([path1 'MLI12_Spikes.mat'])
VStepsMLI=VSteps
clear VSteps

spikeMat1=spikes(l1,:)'; %logical index vector for MLI1
 spikeMat2=spikes(l2,:)';%logical index vector for MLI2
 spikeMat3=spikeMat3';
 
 spikeMat1 = spikeMat1(:,~isnan(spikeMat1(1,:)));
 spikeMat2 = spikeMat2(:,~isnan(spikeMat2(1,:)));
 %Trim to remove negative steps and steps higher than 30 pA (to compare
 %with CC range of steps)
 spikeMat1 = spikeMat1((VStepsMLI>=0 & VStepsMLI<=30),:);
 spikeMat2 = spikeMat2((VStepsMLI>=0 & VStepsMLI<=30),:);
 spikeMat3 = spikeMat3((VStepsCC>=0 & VStepsCC<=30),:);

 VStepsMLI = VStepsMLI(VStepsMLI>=0 & VStepsMLI<=30);
 VStepsCC = VStepsCC(VStepsCC>=0 & VStepsCC<=30);
 
 
 %remove increments of 5 pA in CCs Vsteps and Spikemat3
 spikeMat3=spikeMat3([1:2:7],:);
 spikeMat3=round(spikeMat3);
 VStepsCC=VStepsCC([1:2:7]);
 
 
 n1 = size(spikeMat1,2);
 n2 = size(spikeMat2,2);
 n3 = size(spikeMat3,2);
 ncMLI = size(spikeMat1,1);
 ncCC = size(spikeMat3,1);
 %% run pairwise glme first  MLI1 vs CCs
  cell1IDs = reshape(repmat(1:n1,[ncMLI 1]),[],1); % MLI1
 cell3IDs=  reshape(repmat(n1+1:n1+n3,[ncCC 1]),[],1); %CCs
  cellIDmat = num2str([cell1IDs;cell3IDs]);

 VstepmatMLI1 = reshape(repmat(VStepsMLI',1,n1),[],1);
 VstepmatCC = reshape(repmat(VStepsCC',1,n3),[],1);
 Vstepmat=[VstepmatMLI1 ; VstepmatCC];

 celltype = num2str([ones(n1*ncMLI,1);3*ones(n3*ncCC,1)]);

 dat = table(cellIDmat,[reshape(spikeMat1,[],1); reshape(spikeMat3, [],1)],Vstepmat,celltype,'VariableNames',{'cellID','spkn','vsteps','celltype'});
%  glmfit = fitglme(dat,'spkn~1+vsteps*celltype+(1+vsteps|cellID)','Distribution','Poisson','Link','log');
  glmfit = fitglme(dat,'spkn~1+vsteps*celltype','Distribution','Poisson','Link','log');

 disp(glmfit)
 
 
 
 %% run MLI2 vs CCs
 
 cell2IDs = reshape(repmat(1:n2,[ncMLI 1]),[],1); % MLI2
 cell3IDs=  reshape(repmat(n2+1:n2+n3,[ncCC 1]),[],1); %CCs
 clear cellIDmat
 cellIDmat = num2str([cell2IDs;cell3IDs]);

 VstepmatMLI1 = reshape(repmat(VStepsMLI',1,n2),[],1);
 VstepmatCC = reshape(repmat(VStepsCC',1,n3),[],1);
 Vstepmat=[VstepmatMLI1 ; VstepmatCC];

 celltype = num2str([ones(n2*ncMLI,1);3*ones(n3*ncCC,1)]);

 dat = table(cellIDmat,[reshape(spikeMat2,[],1); reshape(spikeMat3, [],1)],Vstepmat,celltype,'VariableNames',{'cellID','spkn','vsteps','celltype'});
 glmfit = fitglme(dat,'spkn~1+vsteps*celltype','Distribution','Poisson','Link','log');
 disp(glmfit)
 
 
 
 
 
 
 
 
 
 